#include <string.h>

#include "cgic.h"

#include "sqlite3.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

sqlite3 *data_db;


//CREATE TABLE time(ID INTEGER PRIMARY KEY AUTOINCREMENT,[CreatedTime] TimeStamp NOT NULL DEFAULT (datetime('now','localtime')),name text, data text) ;


int operation_data(long msgtype, char buf_type)
{
	char *errmsg;
	char cmd[256];	
	char data[64];
	char name[64];
	int fd;
	
	if (msgtype == 1L ||msgtype == 2L)
	{
		switch (buf_type)
		{
			case 0x11:
				stpcpy(data,"A9_led2_on");
				break;
			case 0x10:
				stpcpy(data,"A9_led2_off");
				
				break;
			case 0x22:
				stpcpy(data,"A9_led3_on");
				
				break;
			case 0x20:
			stpcpy(data,"A9_led3_off");
				
				break;
			case 0x1:
			stpcpy(data,"A9_beep_on");
				
				break;
			case 0x0:
			stpcpy(data,"A9_beep_off");
				
				break;
			default:
				break;
		}
	}else if(msgtype == 3L){
		stpcpy(data,"login");
	}else{

		switch (buf_type)
		{
			case 0xa1:
			stpcpy(data,"M0_led_on");
				
				break;
			case 0xa0:
			stpcpy(data,"M0_led_off");
				
				break;
			case 0x91:
			stpcpy(data,"M0_beep_on");
			
				break;
			case 0x90:
			stpcpy(data,"M0_beep_off");
				
				break;
			case 0xb8:
			stpcpy(data,"M0_seg_on");
				
				break;
			case 0xbf:
			stpcpy(data,"M0_seg_off");
				
				break;
			case 0x81:
			stpcpy(data,"M0_fan_1_on");
				
				break;
			case 0x82:
			stpcpy(data,"M0_fan_2_on");
				
				break;
			case 0x83:
			stpcpy(data,"M0_fan_3_on");
				
				break;
			case 0x80:
			stpcpy(data,"M0_fan_off");
				break;
			default:
				break;
		}
		
	}
	
	fd = open("/a.txt", O_RDONLY);
	if(fd == -1)
	{
		printf("fail to fopen\n");
		return 0;
	}
	read(fd,name,sizeof(name));
	close(fd);
	
	if(sqlite3_open("/login.db", &data_db) != SQLITE_OK)
	{
		printf("sqlite3_open err ");
		return -1;
	}

	sprintf(cmd, "insert into time(name,data) values('%s', '%s')",name, data);
	if(sqlite3_exec(data_db, cmd, NULL, NULL, &errmsg) != SQLITE_OK)
	{
		printf("insert err %s\n",errmsg);
		return -1;
	}
	sqlite3_close(data_db);
	return 0;
}








